/*function createMyDiv() {

    var div = document.createElement("div");
	div.id = "wrap";

	// Move the body's children into this wrapper
	while (document.body.firstChild)
	{
		div.appendChild(document.body.firstChild);
	}

	// Append the wrapper to the body
	document.body.appendChild(div);
	document.getElementById("wrap").style.marginTop = "50px";

	var wrapdiv = document.createElement("div");
	wrapdiv.id = "body-wrap";

	// Move the body's children into this wrapper
	while (document.body.firstChild)
	{
		wrapdiv.appendChild(document.body.firstChild);
	}

	// Append the wrapper to the body
	document.body.appendChild(wrapdiv);


	//document.getElementById("wrap").style.position = "absolute";
	//document.getElementById("wrap").style.top = "50px";

	var myDiv = document.createElement('div');
	myDiv.id = 'my_div';

	var btn = document.createElement("BUTTON");        // Create a <button> element
	var t = document.createTextNode("YES");       // Create a text node
	btn.appendChild(t);                                // Append the text to <button>
	btn.class = "my_popup_close btn btn-default";
	btn.id = "my_popup_response_yes";
	myDiv.appendChild(btn);

	btn = document.createElement("BUTTON");        // Create a <button> element
	t = document.createTextNode("NO");       // Create a text node
	btn.appendChild(t);                                // Append the text to <button>
	btn.class = "my_popup_close btn btn-default";
	btn.id = "my_popup_response_no";
	myDiv.appendChild(btn);

	//document.getElementsByTagName('body')[0].appendChild(myDiv);
	wrapdiv.insertBefore(myDiv,div);

	//div.insertBefore(myDiv,document.body.firstChild);

	document.getElementById("my_div").style.position = "fixed";
	document.getElementById("my_div").style.top = "0px";
	document.getElementById("my_div").float = "center";


}

var timer = setTimeout(createMyDiv,5000);
*/

setInterval(focus_check, 300 * 1000);

var start_focus_time = undefined;
var last_user_interaction = undefined;
var total_active_time = 0;

function focus_check() {

    if (start_focus_time != undefined) {
        var curr_time = new Date();
        //Lets just put it for 4.5 minutes
    if((curr_time.getTime() - last_user_interaction.getTime()) > (270 * 1000)) {
            //No interaction in this tab for last 5 minutes. Probably idle.
            window_unfocused();
        }
    }
}

function window_focused(eo) {

    last_user_interaction = new Date();
    if (start_focus_time == undefined) {
    start_focus_time = new Date();
    }
}

function window_unfocused(eo) {
    if (start_focus_time != undefined) {
    var stop_focus_time = new Date();
    var total_focus_time = stop_focus_time.getTime() - start_focus_time.getTime();
    start_focus_time = undefined;
	total_active_time += total_focus_time;
	//console.log("Time: " + total_focus_time + " " + total_active_time);
	document.getElementById('activeTimeDiv').innerHTML = total_active_time;
    /*var message = {};
        message.type = "time_spent";
        message.domain = document.domain;
        message.time_spent = total_focus_time;
		chrome.extension.sendMessage("", message);
    }*/
	}
}


function createMyDiv() {
var myDiv = document.createElement('div');
	myDiv.id = 'my_div';

	var element = document.createElement("b");
	element.innerHTML = "Did you find what you were looking for?  ";
	element.style.color = "white";
	element.style.fontSize = "18px";
    myDiv.appendChild(element);

	var btn = document.createElement("BUTTON");        // Create a <button> element
	var t = document.createTextNode("YES");       // Create a text node
	btn.appendChild(t);                                // Append the text to <button>
	btn.className = "myButton";
	btn.id = "yes";
	btn.onclick = function () {
	    document.getElementById("my_div").style.visibility = "hidden";
	};
	myDiv.appendChild(btn);

	btn = document.createElement("BUTTON");        // Create a <button> element
	t = document.createTextNode("NO");       // Create a text node
	btn.appendChild(t);                                // Append the text to <button>
	btn.className = "myButton";
	btn.id = "no";
	btn.onclick = function () {
	    document.getElementById("my_div").style.visibility = "hidden";
	};
	myDiv.appendChild(btn);

	var closeSpan = document.createElement('span');
	closeSpan.id = "closeFloatingDiv";
	closeSpan.innerHTML = "x";
	closeSpan.className = "myButton";
	closeSpan.onclick = function () {
		document.getElementById("my_div").style.visibility = "hidden";
	};
	myDiv.appendChild(closeSpan);

	var timeSpan = document.createElement('span');
	timeSpan.id = "activeTimeDiv";
	timeSpan.innerHTML = "0";
	timeSpan.className = "myButton";

	myDiv.appendChild(timeSpan);

	window.addEventListener('load', window_focused, true);
	window.addEventListener('unload', window_unfocused, true);
	window.addEventListener('focus', window_focused, true);
	window.addEventListener('blur', window_unfocused, true);

	window.onkeyup = function (event) {
		if (event.keyCode == 27) {
		    if(document.getElementById("my_div").style.visibility != "hidden") {
				document.getElementById("my_div").style.visibility = "hidden";
			} else {
				document.getElementById("my_div").style.visibility = "visible";
			}
		}
	}

	document.getElementsByTagName('body')[0].appendChild(myDiv);

	//draggable('my_div');
};

createMyDiv();

var dragObj = null;
function draggable(id)
{
    var obj = document.getElementById(id);
    obj.style.position = "absolute";
    obj.onmousedown = function(){
            dragObj = obj;
    }
}

document.onmouseup = function(e){
    dragObj = null;
};

document.onmousemove = function(e){
    var x = e.pageX;
    var y = e.pageY;

    if(dragObj == null)
        return;

    dragObj.style.left = x +"px";
    dragObj.style.top= y +"px";
}; 	